package com.niit.onlinemusicserver34.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.niit.onlinemusicserver34.common.R;
import com.niit.onlinemusicserver34.model.domain.Consumer;
import com.niit.onlinemusicserver34.model.request.AdminRequest;
import com.niit.onlinemusicserver34.model.request.ConsumerRequest;
import com.niit.onlinemusicserver34.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;
    /**
     *管理界面调用：返回所有用户
     */
    @GetMapping("/user")
    public R allUser() {
        return consumerService.allUser();
    }

    @GetMapping("/userbyId")
    public R userById(Integer id) {
        return consumerService.getById(id);
    }
    /**
     *删除用户
     */
    @GetMapping("/user/delete")
    public R deleteUser(@RequestParam int id) {
        return consumerService.deleteUser(id);
    }

    /**
     *批量删除用户
     */
    @DeleteMapping("/user/deleteByIds")
    public R deleteUser(@RequestBody List<Integer> ids) {
        if(consumerService.removeByIds(ids)){
            return R.success("删除成功！");
        }
        return R.success("删除失败！");
    }

    /**
     * 分页查询用户数据
     */
    @GetMapping("/userByPage")
    public R allUserByPage(@RequestParam(defaultValue = "1") int curPage,@RequestParam(defaultValue = "1") int pageSize) {
        IPage<Consumer> page = new Page<>();
        //设置当前页
        page.setCurrent(curPage);
        //设置每页显示数据条数
        page.setSize(pageSize);
        return consumerService.allUser(page);
    }

    @PostMapping("/user/login/status") // http协议 报文的body部分
    public R loginStatus(@RequestBody ConsumerRequest consumerRequest, HttpSession session) {
        R result = consumerService.verifyPasswd(consumerRequest);
        return result;
    }

    /**
     * TODO 前台页面调用 注册
     * 用户注册
     */
    @PostMapping("/user/add")
    public R addUser(@RequestBody ConsumerRequest registryRequest) {
        return consumerService.addUser(registryRequest);
    }

    /**
     * 前后台界面调用
     * 更新用户信息
     */
    @PostMapping("/user/update")
    public R updateUserMsg(@RequestBody ConsumerRequest updateRequest) {
        return consumerService.updateUserMsg(updateRequest);
    }

    /**
     * 更新用户头像
     */
    @PostMapping("/user/avatar/update")
    public R updateUserPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") int id) {
        return consumerService.updateUserAvator(avatorFile, id);
    }

    /**
     * 前后台更新用户密码
     */
    @PostMapping("/user/updatePassword")
    public R updatePassword(@RequestBody ConsumerRequest updatePasswordRequest) {
        return consumerService.updatePassword(updatePasswordRequest);
    }
    public String testSession(HttpServletRequest request, HttpServletResponse response){

        HttpSession session = request.getSession();
        //        System.out.println(session.getId());

        Cookie[] cookies = request.getCookies();

        Cookie cookie = new Cookie("sessionId",session.getId());

        response.addCookie(cookie);
        // 如何记住你？
        session.setAttribute("username","zxc");
        return null;
    }
}
